package me.gacl.websocket.JSR356.example;

import me.gacl.websocket.WebSocketClientInfo;

import javax.websocket.Session;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 功能说明：用来存储业务定义的sessionId和连接(Session)的对应关系
 * 利用业务逻辑中组装的sessionId获取有效连接后进行后续操作
 * Created by 孤傲苍狼 on 2016/2/18.
 */
public class SessionUtils {
    public static Map<String, Session> clients = new ConcurrentHashMap<String, Session>();
    public static List<WebSocketClientInfo> webSocketClientInfos = Collections.synchronizedList(new ArrayList<WebSocketClientInfo>());

    /**
     * 将WebSocket客户端对应的Session添加到clients集合中进行保存
     * @param clientName
     * @param session
     * @return clientId
     */
    public static String put(String clientName, Session session){
        String sessionId = session.getId();
        String clientId =getClientId(clientName, sessionId);
        System.out.println("生成的clientId为:"+clientId);
        //根据clientName和sessionId生成一个用于唯一标识一个WebSocket客户端的cilentId
        clients.put(clientId, session);

        WebSocketClientInfo webSocketClientInfo = new WebSocketClientInfo();
        webSocketClientInfo.setClientId(clientId);
        webSocketClientInfo.setSessionId(sessionId);
        webSocketClientInfo.setClientName(clientName);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        webSocketClientInfo.setConnTime(dateFormat.format(date));
        webSocketClientInfos.add(webSocketClientInfo);
        //返回一个clientId
        return clientId;
    }

    public static Session get(String clientId){
        return clients.get(clientId);
    }

    public static void remove(String clientName,Session session){
        String sessionId = session.getId();
        String clientId =getClientId(clientName, sessionId);
        clients.remove(clientId);
        removeWebSocketClientInfo(clientId);
    }

    public static void remove(String clientId){
        clients.remove(clientId);
        removeWebSocketClientInfo(clientId);
    }

    private static void removeWebSocketClientInfo(String clientId){
        WebSocketClientInfo targetWebSocketClientInfo = null;

        for(WebSocketClientInfo webSocketClientInfo:webSocketClientInfos){
            if (webSocketClientInfo.getClientId().equals(clientId)){
                targetWebSocketClientInfo = webSocketClientInfo;
                break;
            }
        }
        if (targetWebSocketClientInfo!=null){
            webSocketClientInfos.remove(targetWebSocketClientInfo);
        }
    }

    /**
     * 判断是否有连接
     * @param clientId
     * @return
     */
    public static boolean hasConnection(String clientId) {
        if (clientId==null || clientId.equals("")){
            return false;
        }
        return clients.containsKey(clientId);
    }

    /**
     * 根据clientName和sessionId生成一个用于唯一标识一个WebSocket客户端的cilentId
     * @param clientName
     * @param sessionId
     * @return
     */
    private static String getClientId(String clientName,String sessionId){
        return clientName+"_"+sessionId;
    }

    /**
     * 销毁所有连接上来的WebSocket的Session
     */
    public static void releaseAllSession(){
        for(Map.Entry<String,Session> entry:clients.entrySet()){
            Session session = entry.getValue();
            try {
                //关闭WebSocket
                session.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        clients.clear();
        webSocketClientInfos.clear();
    }
}
